package com.adjaisd.scarecrow.testJava;

import com.adjaisd.scarecrow.dto.QuotaAllInfo;
import com.adjaisd.scarecrow.dto.QuotaInfo;
import com.adjaisd.scarecrow.entity.Alarm;
import com.adjaisd.scarecrow.entity.Quota;
import com.adjaisd.scarecrow.service.AlarmService;
import com.adjaisd.scarecrow.service.QuotaService;
import com.adjaisd.scarecrow.vo.Pager;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@SpringBootTest
public class testAlarm {

    @Autowired
    AlarmService alarmService;

    @Test
    void testAlarmLog() {
        Long userId=1L;
        Long page =1L;
        long pageSize=10L;
        String alarmName="";
        String deviceId="";
        String start = "2022-04-04 00:00:00";
        String end = "2022-04-05 00:00:00";
        Pager<QuotaAllInfo> pager = alarmService.queryAlarmLogByUID(userId, page, pageSize, start, end, alarmName, deviceId);
        List<QuotaAllInfo> items = pager.getItems();
        items.forEach(System.out::println);
    }

    @Autowired
    QuotaService quotaService;

    @Test
    void addAlarmData() throws InterruptedException {
        long time = new Date().getTime();
        for (int i = 0; i < 200; i++) {
            Quota quota = quotaService.getById(i);
            if (quota!=null && quota.getCreateTime() > 0) {
                if (quota.getName().equals("温度")) {
                    int level = (int) (Math.random() * 8);
                    String max = String.format("%.2f", 30 + Math.random() * level);
                    String min = String.format("%.2f", -3 - Math.random() * level);
                    Alarm alarm1 = new Alarm(null, "温度过高", ">", Double.parseDouble(max), level, 1.5, "...", time, quota.getId());
                    Alarm alarm2 = new Alarm(null, "温度过低", "<", Double.parseDouble(min), level, 1.5, "...", time, quota.getId());
                    alarmService.addAlarm(alarm1);
                    alarmService.addAlarm(alarm2);
                } else if (quota.getName().equals("湿度")) {
                    int level = (int) (Math.random() * 8);
                    String max = String.format("%.2f", 60 + Math.random() * level * 2);
                    String min = String.format("%.2f", 20 - Math.random() * level * 2);
                    Alarm alarm1 = new Alarm(null, "过于潮湿", ">", Double.parseDouble(max), level, 1.5, "...", time, quota.getId());
                    Alarm alarm2 = new Alarm(null, "过于干燥", "<", Double.parseDouble(min), level, 1.5, "...", time, quota.getId());
                    alarmService.addAlarm(alarm1);
                    alarmService.addAlarm(alarm2);
                }
                Thread.sleep(20);
            }
        }
    }

    private void printQuotaAllInfo(QuotaAllInfo quotaAllInfo) {
        QuotaInfo quotaInfo = new QuotaInfo();
        BeanUtils.copyProperties(quotaAllInfo, quotaInfo);
        System.out.println(quotaAllInfo.getTime() + ", " + quotaInfo);
    }

}
